package io.xxx.omni.osc.sync

import com.alibaba.fastjson.JSONObject
import io.xxx.omni.osc.common.dateTimeFormatter
import io.xxx.omni.osc.common.toJSONObject
import io.xxx.omni.osc.common.toLocalDateTime
import io.xxx.omni.osc.domain.Document
import io.xxx.omni.osc.domain.DocumentType
import io.xxx.omni.osc.domain.PlatformIdentity
import io.xxx.omni.osc.domain.Store
import io.xxx.omni.osc.sdk.HzwClient
import org.springframework.stereotype.Component
import java.time.LocalDateTime

/**
 * 孩子王
 */
abstract class HzwPorter : GenericPageDocumentPorter(), PlatformIdentity.HaiZiWang {

    protected abstract val path: String

    override fun getParameters(store: Store): Pair<List<Any>, Boolean> = listOf(2) to false

    override fun getResponse(context: JobContext, pageNo: Int, hasTotal: Boolean): JSONObject {
        val (store, startTime, endTime, parameter) = context
        val request = buildRequest(startTime, endTime, parameter, pageNo)
        val client = applicationContext.getBean(HzwClient::class.java)
        val response = client.execute(store, path, request)
        if (response.getString("errorCode") != "0") {
            throwException(context, path, response)
        }
        return response
    }

    override fun getCount(context: JobContext, response: JSONObject): Int = response.getInteger("totalNum")

    private fun buildRequest(
        startTime: LocalDateTime,
        endTime: LocalDateTime,
        timeParamType: Any?, // 1. 创建时间，2. 更新时间
        pageNo: Int,
    ): Map<String, Any> {
        return mapOf(
            "startTime" to startTime.format(dateTimeFormatter),
            "endTime" to endTime.format(dateTimeFormatter),
            "page" to pageNo,
            "pageSize" to pageSize,
            "timeParamType" to timeParamType!!
        )
    }
}

// 订单
@Component
class HzwTradePorter : HzwPorter() {

    override val documentType = DocumentType.TRADE

    override val path = "order.list"

    override fun buildDocuments(context: JobContext, response: JSONObject): List<Document> {
        return response
            .getJSONArray("data")
            .map {
                val value = it.toJSONObject()
                val sn = value.getString("dealCode")
                val status = value.getString("status")
                val modified = value.getLong("LastUpdateTime").toLocalDateTime()
                Document(sn, status, modified, value)
            }
    }
}

// 退单
@Component
class HzwRefundPorter : HzwPorter() {

    override val documentType = DocumentType.REFUND

    override val path = "refund.orderList"

    override fun buildDocuments(context: JobContext, response: JSONObject): List<Document> {
        return response
            .getJSONArray("data")
            .map {
                val value = it.toJSONObject()
                val sn = value.getString("dealCode")
                val rsn = value.getString("dealCode")
                val status = value.getString("status")
                val modified = value.getLong("LastUpdateTime").toLocalDateTime()
                Document(sn, rsn, status, modified, value)
            }
    }
}